;
! function(factory) {
    "use strict";
    if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
        var target = module['exports'] || exports;
        factory(target);
    } else if (typeof define === 'function' && define['amd']) {
        define(['exports'], factory);
    } else {
        factory(window['$api'] = {
            isTargetType: function(obj, typeString) {
                return typeof obj === typeString;
            },
            isObject: function(obj) {
                var that = this;
                return (that.isTargetType(obj, "object") && obj != null && obj != undefined);
            },
            isFunction: function(obj) {
                var that = this;
                return that.isTargetType(obj, "function");
            },
            isNumber: function(str) {
                return !isNaN(str);
            },
            isString: function(obj) {
                var that = this;

                return that.isTargetType(obj, "string") && obj != null && obj != undefined;
            },
            isBoolean: function(obj) {
                var that = this;
                return that.isTargetType(obj, "boolean");
            },
            isArray: function(arr) {
                return (toString.apply(arr) === '[object Array]') || arr instanceof NodeList;
            },
            unique: function(arr) {
                var that = this;

                var result = [],
                    hash = {};
                for (var i = 0, elem;
                    (elem = arr[i]) != null; i++) {
                    if (!hash[elem]) {
                        result.push(elem);
                        hash[elem] = true;
                    }
                }
                return result;
            },
            //配置
            DEFAULT_CONFIG: {
                openWin_CONFIG: {
                    bounces: false,
                    bgColor: "rgba(0,0,0,0)",
                    scrollToTop: true,
                    vScrollBarEnabled: false,
                    hScrollBarEnabled: false,
                    scaleEnabled: false,
                    slidBackEnabled: true,
                    slidBackType: "edge",
                    animation: {
                        type: "movein", //动画类型（详见动画类型常量）
                        subType: "from_right", //动画子类型（详见动画子类型常量）
                        duration: 300 //动画过渡时间，默认300毫秒
                    },
                    delay: 0,
                    reload: true,
                    allowEdit: false,
                    softInputMode: "auto"
                },
                openFrame_CONFIG: {
                    bounces: true,
                    bgColor: "rgba(0,0,0,0)",
                    scrollToTop: true,
                    vScrollBarEnabled: false,
                    hScrollBarEnabled: false,
                    scaleEnabled: false,
                    rect: {
                        x: 0,
                        y: 0,
                        w: 'auto',
                        h: 'auto',
                        marginLeft: 0,
                        marginTop: 0,
                        marginBottom: 0,
                        marginRight: 0
                    },
                    reload: true,
                    allowEdit: false,
                    softInputMode: 'auto'
                },
                animation_CONFIG: {
                    delay: 0,
                    duration: 0,
                    curve: "ease_in_out",
                    repeatCount: 0,
                    autoreverse: false
                },
                ajax_CONFIG: {
                    method: "get",
                    cache: false,
                    timeout: 15,
                    dataType: "json",
                    charset: "utf-8",
                    report: false,
                    returnAll: false
                },
            },
            //Apicloud应用
            customMsg: {
                noData: "暂无数据",
                noGoodsData: "暂无商品",
                noAddressData: "您还未添加任何收货地址",
                noShippingList: "暂无配送方式数据",
                noParyList: "暂无支付方式数据",
                noGoodDetail: "该商品暂无数据",
                noGoodReviewCount: "暂无评论数量",
                noGoodReviewList: "暂无评论数据",
                noCateBrand: "分类下暂无品牌数据",
                noPromotionGoods: "暂无商品数据",
                noCateData: "暂无分类数据",
                noSonCateData: "暂无子分类数据",
                noNewsData: "暂无快报数据",
                fullRufundQuantity: "超过订单商品的购买数量啦",
                noFavoriteData: "您还未收藏任何商品",
                noMsg: "您还未收到任何消息",
                noOrderData: "您暂未产生任何订单信息",
                noReturnInfo: "暂无售后信息",
            },

            dblclickToCloseApp: function(callback) {
                var that = this;
                var mkeyTime = new Date().getTime();
                api.addEventListener({
                    name: 'keyback'
                }, function(ret, err) {
                    if ((new Date().getTime() - mkeyTime) > 2000) {
                        mkeyTime = new Date().getTime();
                        api.toast({
                            msg: "再按一次退出",
                            duration: 2000,
                            location: 'bottom'
                        });
                    } else {
                        if (that.isFunction(callback)) {
                            callback();
                        }
                        setTimeout(function() {
                            api.closeWidget({
                                silent: true
                            });
                        }, 300);
                    }
                });
            },
            openWin: function(name) {
                var delay = 0;
                if (api.systemType != 'ios') {
                    delay = 300;
                }
                api.openWin({
                    name: '' + name + '',
                    url: '' + name + '.html',
                    bounces: false,
                    delay: delay,
                    slidBackEnabled: true,
                    vScrollBarEnabled: false
                });
            },
            openFrame: function(frameName, frameUrl, framePageParam, options) {
                var that = this;
                var o = {};
                o.name = frameName;
                o.url = frameUrl;
                o.pageParam = that.isObject(framePageParam) ? framePageParam : {};
                o.delay = 0;
                if (api.systemType != 'ios') {
                    o.delay = 300;
                }
                options = options || {};
                var opt = that.deepAssign({}, that.DEFAULT_CONFIG.openFrame_CONFIG, o, options);

                api.openFrame(opt);
            },
            animation: function(callback, frameName, options) {
                var that = this;
                var o = {};
                if (frameName) {
                    o.name = frameName;
                }
                o.delay = 0;
                if (api.systemType != 'ios') {
                    o.delay = 300;
                }
                options = options || {};
                var opt = that.extendObj(that.DEFAULT_CONFIG.animation_CONFIG, o, options);

                api.animation(opt, function(ret, err) {
                    if (that.isFunction(callback)) {
                        callback(ret, err);
                    }
                });
            },
            auth: function(callback) {
                api.showProgress({
                    style: 'default',
                    animationType: 'fade',
                    text: '认证中...',
                    modal: false
                });
                var that = this;
                var o = {
                    url: that.serverUrl + 'auth',
                    method: 'post',
                    timeout: 300,
                    headers: {},
                    data: {
                        values: {
                            userName: 'admin',
                            password: 'admin'
                        }
                    }
                };
                api.ajax(o, function(ret, err) {
                    console.log('ret' + JSON.stringify(ret));
                    if (ret) {
                        api.hideProgress();
                        api.setGlobalData({
                            key: 'randomKey',
                            value: ret.randomKey
                        });
                        api.setGlobalData({
                            key: 'token',
                            value: ret.token
                        });
                        callback(ret, err);
                    } else {
                        console.log('err' + JSON.stringify(err));
                    }
                });
            },
            ajax: function(callback, url, method, data, dataType, options) {
                var token = api.getGlobalData({
                    key: 'token'
                });
                var randomKey = api.getGlobalData({
                    key: 'randomKey'
                });
                var that = this;
                var o = {
                    data: {}
                };
                o.url = that.serverUrl + url;
                o.method = method ? method : "get";
                o.dataType = dataType ? dataType : "json";
                o.headers = {
                    "Authorization": "Bearer " + token,
                    "Content-Type": "application/json"
                };
                console.log("请求提交参数(编译前):" + JSON.stringify(data));
                o.data.body = {
                    object: base64.encode(JSON.stringify(data)),
                    sign: md5(base64.encode(JSON.stringify(data)) + randomKey)
                }
                console.log("请求提交参数:" + JSON.stringify(o));
                api.ajax(o, function(ret, err) {
                    if (that.isFunction(callback)) {
                        if (err) {
                            console.log(url)
                            try {
                                myLayer.clear();
                            } catch (e) {
                                console.log(JSON.stringify(e))
                            }
                            api.refreshHeaderLoadDone();
                            api.hideProgress();
                            console.log(JSON.stringify(err))
                            if (err.body.code == 700) {
                                api.toast({
                                    msg: url + "网络请求超时",
                                    location: 'middle'
                                });
                            } else {
                                api.toast({
                                    msg: "数据获取失败。请求地址：" + url + "。请求数据：" + JSON.stringify(data) + "详情：" + JSON.stringify(err),
                                    location: 'middle'
                                });
                            }
                        } else if (typeof ret.code != 'undefined' && ret.code == 700) {
                            var delay = 0;
                            if (api.systemType != 'ios') {
                                delay = 300;
                            }
                            api.openWin({
                                name: 'error',
                                url: 'error.html',
                                bounces: false,
                                delay: delay,
                                slidBackEnabled: true,
                                vScrollBarEnabled: false
                            });
                        } else {
                            console.log("===ajax success ret================")
                            callback(ret, err);
                        }
                    }
                });
            },
            uploadFile: function(callback, data, dataType, options) {
                var that = this;
                var o = {
                    data: {}
                };
                o.url = that.serverUrl + "fileUpload/upload";
                o.method = "post";
                o.dataType = dataType ? dataType : "json";
                o.data.values = {}
                o.data.files = {
                    file: data
                }
                console.log("请求提交参数:" + JSON.stringify(o));
                api.ajax(o, function(ret, err) {
                    if (that.isFunction(callback)) {
                        if (err) {
                            try {
                                myLayer.clear();
                            } catch (e) {
                                console.log(JSON.stringify(e))
                            }
                            api.refreshHeaderLoadDone();
                            api.hideProgress();
                            console.log(JSON.stringify(err))
                            if (err.msg == "网络请求超时，请稍后重试") {
                                api.toast({
                                    msg: "网络请求超时",
                                    location: 'middle'
                                });
                            } else {
                                api.toast({
                                    msg: "数据获取失败",
                                    location: 'middle'
                                });
                            }
                        } else {
                            console.log("===ajax success ret================")
                            callback(ret, err);
                        }
                    }
                });
            },
            ajaxWithoutDecode: function(callback, url, method, data, dataType, options) {
                var that = this;
                var o = {
                    data: {}
                };
                o.url = that.serverUrl + url;
                o.method = method ? method : "get";
                o.dataType = dataType ? dataType : "json";
                o.data.body = data
                console.log("请求提交参数:" + JSON.stringify(o));
                api.ajax(o, function(ret, err) {
                    if (that.isFunction(callback)) {
                        if (err) {
                            console.log(url)
                            try {
                                myLayer.clear();
                            } catch (e) {
                                console.log(JSON.stringify(e))
                            }
                            api.refreshHeaderLoadDone();
                            api.hideProgress();
                            console.log(JSON.stringify(err))
                            if (err.msg == "网络请求超时，请稍后重试") {
                                api.toast({
                                    msg: "网络请求超时",
                                    location: 'middle'
                                });
                            } else {
                                api.toast({
                                    msg: "数据获取失败",
                                    location: 'middle'
                                });
                            }
                        } else {
                            console.log("===ajax success ret================")
                            callback(ret, err);
                        }
                    }
                });
            },
            readFile: function(callback, path) {
                var that = this;
                api.readFile({
                    path: path
                }, function(ret, err) {
                    if (that.isFunction(callback)) {
                        callback(ret, err);
                    }
                });
            },
            writeFile: function(callback, path, data, isAppend) {
                var that = this;
                isAppend = that.isTargetType(isAppend, "boolean") ? isAppend : false;
                if (that.isObject(data)) {
                    data = JSON.stringify(data);
                }
                api.writeFile({
                    path: path,
                    data: data,
                    append: isAppend
                }, function(ret, err) {
                    if (that.isFunction(callback)) {
                        callback(ret, err);
                    }
                });
            },
            //缓存数据
            doCache: function(callback, path, url, method, data) {
                var count = 1
                var that = this;
                var url = url
                var path = api.cacheDir + path;
                //console.log("doCache:"+data);
                that.readFile(function(ret, err) {
                    if (ret.status) {
                        console.log("===daCache readFile success ret================")
                        count++
                        console.log(count)
                        var cacheData = ret.data;
                        callback(JSON.parse(cacheData));
                        //再远程取一下数据，防止有更新
                        count++
                        console.log(count)
                        that.ajax(function(ret, err) {
                            if (ret) {
                                console.log("===daCache confirm success ret================")
                                if (cacheData != JSON.stringify(ret)) {
                                    console.log("cacheData != JSON.stringify(ret)")
                                    callback(ret);
                                    //缓存数据
                                    that.writeFile(function() {}, path, ret);
                                }
                            } else {
                                console.log("===daCache confirm fail ret================")
                                console.log(url)
                                try {
                                    myLayer.clear();
                                } catch (e) {

                                }
                                api.hideProgress();
                                api.refreshHeaderLoadDone();
                                if (err.msg == "网络请求超时，请稍后重试") {
                                    api.toast({
                                        msg: "网络缓存请求超时",
                                        location: 'middle'
                                    });
                                } else {
                                    alert("325");
                                    api.toast({
                                        msg: "数据获取失败",
                                        location: 'middle'
                                    });
                                }
                            }
                        }, url, method, data);
                    } else {
                        console.log("===daCache readFile fail err================")
                            //如果失败则从服务器读取
                        that.ajax(function(ret, err) {
                            if (ret) {
                                console.log("===daCache ajax success ret================")
                                console.log(url)
                                callback(ret);
                                //缓存数据
                                that.writeFile(function() {}, path, ret);
                            } else {
                                console.log("===daCache ajax fail ret================")
                                console.log(url)
                                try {
                                    myLayer.clear();
                                } catch (e) {

                                }
                                api.hideProgress();
                                api.refreshHeaderLoadDone();
                                if (err.msg == "网络请求超时，请稍后重试") {
                                    api.toast({
                                        msg: "网络请求超时",
                                        location: 'middle'
                                    });
                                } else {
                                    alert("359");
                                    api.toast({
                                        msg: "数据获取失败",
                                        location: 'middle'
                                    });
                                }
                            }
                        }, url, "post", data);
                    }
                }, path);
            },
            //下载缓存图片
            cacheImage: function(datas, param, folder, index) {
                var that = this;
                if (datas.length <= index) {
                    return;
                }
                var url = datas[index][param];
                var pos = url.lastIndexOf("/");
                var filename = url.substring(pos + 1);
                var path = api.cacheDir + '/pic/' + folder + '/' + filename;
                var obj = api.require('fs');
                obj.exist({
                    path: path
                }, function(ret, err) {
                    if (ret.exist) {
                        if (ret.directory) {
                            api.download({
                                url: url,
                                savePath: path,
                                report: false,
                                cache: true,
                                allowResume: true
                            }, function() {});
                        }
                    } else {
                        api.download({
                            url: url,
                            savePath: path,
                            report: false,
                            cache: true,
                            allowResume: true
                        }, function() {});
                    }
                    that.cacheImage(datas, param, folder, index + 1);
                });
            },
            //缓存图片加载zepto
            iCache: function() {
                $('.icache').each(function(index) {
                    (function(param) {
                        var url = $('.icache').eq(param).attr("src");
                        var pos = url.lastIndexOf("/");
                        var filename = url.substring(pos + 1);
                        var path = api.cacheDir + "/pic/List/" + filename;
                        var obj = api.require('fs');
                        obj.exist({
                            path: path
                        }, function(ret, err) {
                            if (ret.exist) {
                                if (ret.directory) {} else {
                                    $('.icache').eq(param).attr('src', null);
                                    path = api.cacheDir + "/pic/List/" + filename;
                                    $('.icache').eq(param).attr('src', path);
                                }
                            } else {
                                api.download({
                                    url: url,
                                    savePath: path,
                                    report: false,
                                    cache: true,
                                    allowResume: true
                                }, function(ret, err) {});
                            }
                        });
                    })(index);
                });
            },
        });
    }
}(function(HelpExports) {
    var u = typeof HelpExports !== 'undefined' ? HelpExports : {};
    Object.defineProperty(u, "serverUrl", {
        get: function() {
            return "http://yxhb.youxi1027.com/rest/";
            // return "http://test.youxi1027.com/rest/";
            // return "http://192.168.1.112/"; //生产
            // return "http://192.168.169.125/"; //生产
            // return "http://192.168.31.119/"; //生产
            // return "http://yxhb.youxi1027.com/rest/"; //生产
            //return "http://172.168.1.4:8088/"; //生产
            //return "http://192.168.168.150:8088/"; //生产
        }
    });

    // 对象深度拷贝
    function isObj(x) {
        var type = typeof x;
        return x !== null && (type === 'object' || type === 'function');
    }
    var hasOwnProperty = Object.prototype.hasOwnProperty;
    var propIsEnumerable = Object.prototype.propertyIsEnumerable;

    function toObject(val) {
        if (val === null || val === undefined) {
            throw new TypeError('Cannot convert undefined or null to object');
        }
        return Object(val);
    }

    function assignKey(to, from, key) {
        var val = from[key];
        if (val === undefined || val === null) {
            return;
        }
        if (hasOwnProperty.call(to, key)) {
            if (to[key] === undefined || to[key] === null) {
                throw new TypeError('Cannot convert undefined or null to object (' + key + ')');
            }
        }
        if (!hasOwnProperty.call(to, key) || !isObj(val)) {
            to[key] = val;
        } else {
            to[key] = assign(Object(to[key]), from[key]);
        }
    }

    function assign(to, from) {
        if (to === from) {
            return to;
        }
        from = Object(from);
        for (var key in from) {
            if (hasOwnProperty.call(from, key)) {
                assignKey(to, from, key);
            }
        }
        if (Object.getOwnPropertySymbols) {
            var symbols = Object.getOwnPropertySymbols(from);
            for (var i = 0; i < symbols.length; i++) {
                if (propIsEnumerable.call(from, symbols[i])) {
                    assignKey(to, from, symbols[i]);
                }
            }
        }
        return to;
    }
    u.deepAssign = function(target) {
        target = toObject(target);
        for (var s = 1; s < arguments.length; s++) {
            assign(target, arguments[s]);
        }
        return target;
    };
    //$api操作
    var isAndroid = (/android/gi).test(navigator.appVersion);
    var uzStorage = function() {
        var ls = window.localStorage;
        if (isAndroid) {
            ls = os.localStorage();
        }
        return ls;
    };

    function parseArguments(url, data, fnSuc, dataType) {
        if (typeof(data) == 'function') {
            dataType = fnSuc;
            fnSuc = data;
            data = undefined;
        }
        if (typeof(fnSuc) != 'function') {
            dataType = fnSuc;
            fnSuc = undefined;
        }
        return {
            url: url,
            data: data,
            fnSuc: fnSuc,
            dataType: dataType
        };
    }
    u.trim = function(str) {
        if (String.prototype.trim) {
            return str == null ? "" : String.prototype.trim.call(str);
        } else {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        }
    };
    u.trimAll = function(str) {
        return str.replace(/\s*/g, '');
    };
    u.isElement = function(obj) {
        return !!(obj && obj.nodeType == 1);
    };
    u.isArray = function(obj) {
        if (Array.isArray) {
            return Array.isArray(obj);
        } else {
            return obj instanceof Array;
        }
    };
    u.isEmptyObject = function(obj) {
        if (JSON.stringify(obj) === '{}') {
            return true;
        }
        return false;
    };

    u.addEvt = function(el, name, fn, useCapture) {
        if (!u.isElement(el)) {
            console.warn('$api.addEvt Function need el param, el param must be DOM Element');
            return;
        }
        useCapture = useCapture || false;
        if (el.addEventListener) {
            el.addEventListener(name, fn, useCapture);
        }
    };
    u.rmEvt = function(el, name, fn, useCapture) {
        if (!u.isElement(el)) {
            console.warn('$api.rmEvt Function need el param, el param must be DOM Element');
            return;
        }
        useCapture = useCapture || false;
        if (el.removeEventListener) {
            el.removeEventListener(name, fn, useCapture);
        }
    };
    u.one = function(el, name, fn, useCapture) {
        if (!u.isElement(el)) {
            console.warn('$api.one Function need el param, el param must be DOM Element');
            return;
        }
        useCapture = useCapture || false;
        var that = this;
        var cb = function() {
            fn && fn();
            that.rmEvt(el, name, cb, useCapture);
        };
        that.addEvt(el, name, cb, useCapture);
    };
    u.dom = function(el, selector) {
        if (arguments.length === 1 && typeof arguments[0] == 'string') {
            if (document.querySelector) {
                return document.querySelector(arguments[0]);
            }
        } else if (arguments.length === 2) {
            if (el.querySelector) {
                return el.querySelector(selector);
            }
        }
    };
    u.domAll = function(el, selector) {
        if (arguments.length === 1 && typeof arguments[0] == 'string') {
            if (document.querySelectorAll) {
                return document.querySelectorAll(arguments[0]);
            }
        } else if (arguments.length === 2) {
            if (el.querySelectorAll) {
                return el.querySelectorAll(selector);
            }
        }
    };
    u.byId = function(id) {
        return document.getElementById(id);
    };
    u.first = function(el, selector) {
        if (arguments.length === 1) {
            if (!u.isElement(el)) {
                console.warn('$api.first Function need el param, el param must be DOM Element');
                return;
            }
            return el.children[0];
        }
        if (arguments.length === 2) {
            return this.dom(el, selector + ':first-child');
        }
    };
    u.last = function(el, selector) {
        if (arguments.length === 1) {
            if (!u.isElement(el)) {
                console.warn('$api.last Function need el param, el param must be DOM Element');
                return;
            }
            var children = el.children;
            return children[children.length - 1];
        }
        if (arguments.length === 2) {
            return this.dom(el, selector + ':last-child');
        }
    };
    u.eq = function(el, index) {
        return this.dom(el, ':nth-child(' + index + ')');
    };
    u.not = function(el, selector) {
        return this.domAll(el, ':not(' + selector + ')');
    };
    u.prev = function(el) {
        if (!u.isElement(el)) {
            console.warn('$api.prev Function need el param, el param must be DOM Element');
            return;
        }
        var node = el.previousSibling;
        if (node.nodeType && node.nodeType === 3) {
            node = node.previousSibling;
            return node;
        }
    };
    u.next = function(el) {
        if (!u.isElement(el)) {
            console.warn('$api.next Function need el param, el param must be DOM Element');
            return;
        }
        var node = el.nextSibling;
        if (node.nodeType && node.nodeType === 3) {
            node = node.nextSibling;
            return node;
        }
    };
    u.closest = function(el, selector) {
        if (!u.isElement(el)) {
            console.warn('$api.closest Function need el param, el param must be DOM Element');
            return;
        }
        var doms, targetDom;
        var isSame = function(doms, el) {
            var i = 0,
                len = doms.length;
            for (i; i < len; i++) {
                if (doms[i].isEqualNode(el)) {
                    return doms[i];
                }
            }
            return false;
        };
        var traversal = function(el, selector) {
            doms = u.domAll(el.parentNode, selector);
            targetDom = isSame(doms, el);
            while (!targetDom) {
                el = el.parentNode;
                if (el != null && el.nodeType == el.DOCUMENT_NODE) {
                    return false;
                }
                traversal(el, selector);
            }

            return targetDom;
        };

        return traversal(el, selector);
    };
    u.contains = function(parent, el) {
        var mark = false;
        if (el === parent) {
            mark = true;
            return mark;
        } else {
            do {
                el = el.parentNode;
                if (el === parent) {
                    mark = true;
                    return mark;
                }
            } while (el === document.body || el === document.documentElement);

            return mark;
        }

    };
    u.remove = function(el) {
        if (el && el.parentNode) {
            el.parentNode.removeChild(el);
        }
    };
    u.attr = function(el, name, value) {
        if (!u.isElement(el)) {
            console.warn('$api.attr Function need el param, el param must be DOM Element');
            return;
        }
        if (arguments.length == 2) {
            return el.getAttribute(name);
        } else if (arguments.length == 3) {
            el.setAttribute(name, value);
            return el;
        }
    };
    u.removeAttr = function(el, name) {
        if (!u.isElement(el)) {
            console.warn('$api.removeAttr Function need el param, el param must be DOM Element');
            return;
        }
        if (arguments.length === 2) {
            el.removeAttribute(name);
        }
    };
    u.hasCls = function(el, cls) {
        if (!u.isElement(el)) {
            console.warn('$api.hasCls Function need el param, el param must be DOM Element');
            return;
        }
        if (el.className.indexOf(cls) > -1) {
            return true;
        } else {
            return false;
        }
    };
    u.addCls = function(el, cls) {
        if (!u.isElement(el)) {
            console.warn('$api.addCls Function need el param, el param must be DOM Element');
            return;
        }
        if ('classList' in el) {
            el.classList.add(cls);
        } else {
            var preCls = el.className;
            var newCls = preCls + ' ' + cls;
            el.className = newCls;
        }
        return el;
    };
    u.removeCls = function(el, cls) {
        if (!u.isElement(el)) {
            console.warn('$api.removeCls Function need el param, el param must be DOM Element');
            return;
        }
        if ('classList' in el) {
            el.classList.remove(cls);
        } else {
            var preCls = el.className;
            var newCls = preCls.replace(cls, '');
            el.className = newCls;
        }
        return el;
    };
    u.toggleCls = function(el, cls) {
        if (!u.isElement(el)) {
            console.warn('$api.toggleCls Function need el param, el param must be DOM Element');
            return;
        }
        if ('classList' in el) {
            el.classList.toggle(cls);
        } else {
            if (u.hasCls(el, cls)) {
                u.removeCls(el, cls);
            } else {
                u.addCls(el, cls);
            }
        }
        return el;
    };
    u.val = function(el, val) {
        if (!u.isElement(el)) {
            console.warn('$api.val Function need el param, el param must be DOM Element');
            return;
        }
        if (arguments.length === 1) {
            switch (el.tagName) {
                case 'SELECT':
                    var value = el.options[el.selectedIndex].value;
                    return value;
                    break;
                case 'INPUT':
                    return el.value;
                    break;
                case 'TEXTAREA':
                    return el.value;
                    break;
            }
        }
        if (arguments.length === 2) {
            switch (el.tagName) {
                case 'SELECT':
                    el.options[el.selectedIndex].value = val;
                    return el;
                    break;
                case 'INPUT':
                    el.value = val;
                    return el;
                    break;
                case 'TEXTAREA':
                    el.value = val;
                    return el;
                    break;
            }
        }

    };
    u.prepend = function(el, html) {
        if (!u.isElement(el)) {
            console.warn('$api.prepend Function need el param, el param must be DOM Element');
            return;
        }
        el.insertAdjacentHTML('afterbegin', html);
        return el;
    };
    u.append = function(el, html) {
        if (!u.isElement(el)) {
            console.warn('$api.append Function need el param, el param must be DOM Element');
            return;
        }
        el.insertAdjacentHTML('beforeend', html);
        return el;
    };
    u.before = function(el, html) {
        if (!u.isElement(el)) {
            console.warn('$api.before Function need el param, el param must be DOM Element');
            return;
        }
        el.insertAdjacentHTML('beforebegin', html);
        return el;
    };
    u.after = function(el, html) {
        if (!u.isElement(el)) {
            console.warn('$api.after Function need el param, el param must be DOM Element');
            return;
        }
        el.insertAdjacentHTML('afterend', html);
        return el;
    };
    u.html = function(el, html) {
        if (!u.isElement(el)) {
            console.warn('$api.html Function need el param, el param must be DOM Element');
            return;
        }
        if (arguments.length === 1) {
            return el.innerHTML;
        } else if (arguments.length === 2) {
            el.innerHTML = html;
            return el;
        }
    };
    u.text = function(el, txt) {
        if (!u.isElement(el)) {
            console.warn('$api.text Function need el param, el param must be DOM Element');
            return;
        }
        if (arguments.length === 1) {
            return el.textContent;
        } else if (arguments.length === 2) {
            el.textContent = txt;
            return el;
        }
    };
    u.offset = function(el) {
        if (!u.isElement(el)) {
            console.warn('$api.offset Function need el param, el param must be DOM Element');
            return;
        }
        var sl = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
        var st = Math.max(document.documentElement.scrollTop, document.body.scrollTop);

        var rect = el.getBoundingClientRect();
        return {
            l: rect.left + sl,
            t: rect.top + st,
            w: el.offsetWidth,
            h: el.offsetHeight
        };
    };
    u.css = function(el, css) {
        if (!u.isElement(el)) {
            console.warn('$api.css Function need el param, el param must be DOM Element');
            return;
        }
        if (typeof css == 'string' && css.indexOf(':') > 0) {
            el.style && (el.style.cssText += ';' + css);
        }
    };
    u.cssVal = function(el, prop) {
        if (!u.isElement(el)) {
            console.warn('$api.cssVal Function need el param, el param must be DOM Element');
            return;
        }
        if (arguments.length === 2) {
            var computedStyle = window.getComputedStyle(el, null);
            return computedStyle.getPropertyValue(prop);
        }
    };
    u.jsonToStr = function(json) {
        if (typeof json === 'object') {
            return JSON && JSON.stringify(json);
        }
    };
    u.strToJson = function(str) {
        if (typeof str === 'string') {
            return JSON && JSON.parse(str);
        }
    };
    u.setStorage = function(key, value) {
        if (arguments.length === 2) {
            var v = value;
            if (typeof v == 'object') {
                v = JSON.stringify(v);
                v = 'obj-' + v;
            } else {
                v = 'str-' + v;
            }
            var ls = uzStorage();
            if (ls) {
                ls.setItem(key, v);
            }
        }
    };
    u.getStorage = function(key) {
        var ls = uzStorage();
        if (ls) {
            var v = ls.getItem(key);
            if (!v) {
                return;
            }
            if (v.indexOf('obj-') === 0) {
                v = v.slice(4);
                return JSON.parse(v);
            } else if (v.indexOf('str-') === 0) {
                return v.slice(4);
            }
        }
    };
    u.rmStorage = function(key) {
        var ls = uzStorage();
        if (ls && key) {
            ls.removeItem(key);
        }
    };
    u.clearStorage = function() {
        var ls = uzStorage();
        if (ls) {
            ls.clear();
        }
    };
    /*by king*/
    u.fixIos7Bar = function(el) {
        if (!u.isElement(el)) {
            console.warn('$api.fixIos7Bar Function need el param, el param must be DOM Element');
            return;
        }
        var strDM = api.systemType;
        if (strDM == 'ios') {
            var strSV = api.systemVersion;
            var numSV = parseInt(strSV, 10);
            var fullScreen = api.fullScreen;
            var iOS7StatusBarAppearance = api.iOS7StatusBarAppearance;
            if (numSV >= 7 && !fullScreen && iOS7StatusBarAppearance) {
                el.style.paddingTop = '20px';
            }
        }
    };
    u.fixStatusBar = function(el) {
        if (!u.isElement(el)) {
            console.warn('$api.fixStatusBar Function need el param, el param must be DOM Element');
            return;
        }
        var sysType = api.systemType;
        if (sysType == 'ios') {
            u.fixIos7Bar(el);
        } else if (sysType == 'android') {
            var ver = api.systemVersion;
            ver = parseFloat(ver);
            if (ver >= 4.4) {
                el.style.paddingTop = '25px';
            }
        }
    };
    u.toast = function(title, text, time) {
        var opts = {};
        var show = function(opts, time) {
            api.showProgress(opts);
            setTimeout(function() {
                api.hideProgress();
            }, time);
        };
        if (arguments.length === 1) {
            var time = time || 500;
            if (typeof title === 'number') {
                time = title;
            } else {
                opts.title = title + '';
            }
            show(opts, time);
        } else if (arguments.length === 2) {
            var time = time || 500;
            var text = text;
            if (typeof text === "number") {
                var tmp = text;
                time = tmp;
                text = null;
            }
            if (title) {
                opts.title = title;
            }
            if (text) {
                opts.text = text;
            }
            show(opts, time);
        }
        if (title) {
            opts.title = title;
        }
        if (text) {
            opts.text = text;
        }
        time = time || 500;
        show(opts, time);
    };
    u.post = function( /*url,data,fnSuc,dataType*/ ) {
        var argsToJson = parseArguments.apply(null, arguments);
        var json = {};
        var fnSuc = argsToJson.fnSuc;
        argsToJson.url && (json.url = argsToJson.url);
        argsToJson.data && (json.data = argsToJson.data);
        if (argsToJson.dataType) {
            var type = argsToJson.dataType.toLowerCase();
            if (type == 'text' || type == 'json') {
                json.dataType = type;
            }
        } else {
            json.dataType = 'json';
        }
        json.method = 'post';
        api.ajax(json,
            function(ret, err) {
                if (ret) {
                    fnSuc && fnSuc(ret);
                }
            }
        );
    };
    u.get = function( /*url,fnSuc,dataType*/ ) {
        var argsToJson = parseArguments.apply(null, arguments);
        var json = {};
        var fnSuc = argsToJson.fnSuc;
        argsToJson.url && (json.url = argsToJson.url);
        //argsToJson.data && (json.data = argsToJson.data);
        if (argsToJson.dataType) {
            var type = argsToJson.dataType.toLowerCase();
            if (type == 'text' || type == 'json') {
                json.dataType = type;
            }
        } else {
            json.dataType = 'text';
        }
        json.method = 'get';
        api.ajax(json,
            function(ret, err) {
                if (ret) {
                    fnSuc && fnSuc(ret);
                }
            }
        );
    };

    // 解决 IOS active问题
    if (document.body) {
        document.body.addEventListener('touchstart', function() {});
    }
});
